using System;
using System.Collections.Generic;
using System.Text;
using ALib;

namespace TradingPlatform
{
    public class BarFormer
    {
        private double _close;
        private double _high;
        private double _low;
        private double _open;
        private double _volume;


        public BarFormer()
        {
            _open = double.NaN;
            _high = double.NaN;
            _low = double.NaN;
            _close = double.NaN;
            _volume = 0;
        }

        public double Open
        {
            get { return _open; }
            set { _open = value; }
        }

        public double High
        {
            get { return _high; }
            set { _high = value; }
        }

        public double Low
        {
            get { return _low; }
            set { _low = value; }
        }

        public double Close
        {
            get { return _close; }
            set { _close = value; }
        }

        public double Volume
        {
            get { return _volume; }
            set { _volume = value; }
        }
        
        public void UpdateWithTickPrice(double lastPrice)
        {
            if (!double.IsNaN(lastPrice)&&lastPrice>0)
            {
                if (double.IsNaN(_open))
                {
                    _open = lastPrice;
                    _high = lastPrice;
                    _low = lastPrice;
                    _close = lastPrice;
                }
                else
                {
                    if (_high < lastPrice)
                    {
                        _high = lastPrice;
                    }
                    if (lastPrice < _low)
                    {
                        _low = lastPrice;
                    }
                    _close = lastPrice;
                }
            }
        }

        public void UpdateWithTickVolume(double volume)
        {
            if (!double.IsNaN(volume) && volume > 0)
            {
                _volume += volume;
            }
        }
    }
}